package top.kaoshanji.p3ch11a;

import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;

/**
 * 代码清单-p3ch11a-352页a：更新volatile对象引用AtomicReferenceFieldUpdater类的使用示例
 * author kaoshanji
 * ^_^
 * create time 2025/2/4 10:56
 */
public class TreeNode {

    private volatile TreeNode parent;

    private static final AtomicReferenceFieldUpdater<TreeNode, TreeNode> parentUpdater =
            AtomicReferenceFieldUpdater.newUpdater(TreeNode.class, TreeNode.class, "parent");

    public boolean compareAndSetParent(TreeNode expect, TreeNode update) {
        return parentUpdater.compareAndSet(this, expect, update);
    }


}
